package com.cty.A_GeneralDataStructures.C_Tree.BinaryTree;

import com.cty.F_common.DataItem;

import java.util.Scanner;

/**
 * @Auther: cty
 * @Date: 2020/6/16 12:02
 * @Description: 测试普通二叉搜索树
 * @version: 1.0
 */
public class BinaryTreeApp {
    public static void main(String[] args) {
        int key;
        BinaryTree tree = new BinaryTree();

        tree.insert(new DataItem(50));
        tree.insert(new DataItem(25));
        tree.insert(new DataItem(75));
        tree.insert(new DataItem(12));
        tree.insert(new DataItem(37));
        tree.insert(new DataItem(43));
        tree.insert(new DataItem(30));
        tree.insert(new DataItem(33));
        tree.insert(new DataItem(87));
        tree.insert(new DataItem(93));
        tree.insert(new DataItem(90));

        while(true)
        {
            System.out.print("Enter first letter of show, insert, find, delete or traverse: ");
            int choice = getChar();
            switch (choice)
            {
                case 's':
                    tree.displayTree();
                    break;
                case 'i':
                    System.out.print("Enter key to insert: ");
                    key = getInt();
                    tree.insert(new DataItem(key));
                    break;
                case 'f':
                    System.out.print("Enter key to find: ");
                    key = getInt();
                    DataItem findItem = tree.find(key);
                    if(findItem != null)
                        System.out.println("Found " + findItem.getKey());
                    else
                        System.out.println("Could not find " + key);
                    break;
                case 'd':
                    System.out.print("Enter a key to delete: ");
                    key = getInt();
                    DataItem delItem = tree.delete(key);
                    if(delItem != null)
                        System.out.println("Deleted " + key);
                    else
                        System.out.println("Could not delete " + key);
                    break;
                case 't':
                    System.out.print("Enter traverse type 1, 2 or 3: ");
                    key = getInt();
                    tree.traverse(key);
                    break;
                default:
                    System.out.println("Invalid entry");
            }  // end switch

        }  // end while true

    }  // end method main

    public static String getString()
    {
        Scanner scan = new Scanner(System.in);
        return scan.nextLine();
    }

    public static char getChar()
    {
        String str = getString();
        return str.charAt(0);
    }

    public static int getInt()
    {
        String str = getString();
        return Integer.parseInt(str);
    }
}  // end BinaryTreeApp{}

/**
 * Enter first letter of show, insert, find, delete or traverse: s
 * ..........................................................................
 *                                 50
 *                 25                              75
 *         12              37              --              87
 *     --      --      30      43      --      --      --      93
 *   --  --  --  --  --  33  --  --  --  --  --  --  --  --  90  --
 * ..........................................................................
 * Enter first letter of show, insert, find, delete or traverse: i
 * Enter key to insert: 6
 * Enter first letter of show, insert, find, delete or traverse: s
 * ..........................................................................
 *                                 50
 *                 25                              75
 *         12              37              --              87
 *     6      --      30      43      --      --      --      93
 *   --  --  --  --  --  33  --  --  --  --  --  --  --  --  90  --
 * ..........................................................................
 * Enter first letter of show, insert, find, delete or traverse: f
 * Enter key to find: 90
 * Found 90
 * Enter first letter of show, insert, find, delete or traverse: d
 * Enter a key to delete: 30
 * Deleted 30
 * Enter first letter of show, insert, find, delete or traverse: s
 * ..........................................................................
 *                                 50
 *                 25                              75
 *         12              37              --              87
 *     6      --      33      43      --      --      --      93
 *   --  --  --  --  --  --  --  --  --  --  --  --  --  --  90  --
 * ..........................................................................
 * Enter first letter of show, insert, find, delete or traverse: t
 * Enter traverse type 1, 2 or 3: 1
 * 50 25 12 6 37 33 43 75 87 93 90
 * Enter first letter of show, insert, find, delete or traverse: t
 * Enter traverse type 1, 2 or 3: 2
 * 6 12 25 33 37 43 50 75 87 90 93
 * Enter first letter of show, insert, find, delete or traverse: t
 * Enter traverse type 1, 2 or 3: 3
 * 6 12 33 43 37 25 90 93 87 75 50
 */
